﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Networking;
using System;

public class ServerGameEvents : MonoBehaviour {

    public int lastHitReceived = 0;
    public int maxEvents = 5;

    public EventQueue eventQueue = new EventQueue();

    private NetworkDungeonController controller = null;

    void Start()
    {
        controller = GameObject.FindGameObjectWithTag("GameController").GetComponent<NetworkDungeonController>();
    }

	// Update is called once per frame
	void Update () {

        if (controller.singlePlayer || (Network.isServer && controller.playerCount == 1))
        {

            for (int i = 0; i < eventQueue.Count; i++)
            {
                // Events that the server writes:
                // Victory
                // Loss
                // Player hit

                GameEvent currentEvent = eventQueue.GetNextEvent();

                switch (currentEvent.EventType)
                {
                    case GameEvent.GameEventType.PlayerHit:
                        // process
                        ProcessPlayerHit(GameObject.FindGameObjectWithTag("Player"), ((PlayerHitGameEvent)currentEvent).damageReceived);
                        break;
                }

            }
        }

	}


    void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    {
        if (stream.isWriting)
        {
            int eventSendCount = maxEvents;
            if (eventSendCount > eventQueue.Count) eventSendCount = eventQueue.Count;

            stream.Serialize(ref eventSendCount);

            for (int i = 0; i < eventSendCount; i++)
            {
                // Events that the server writes:
                // Victory
                // Loss
                // Player hit

                GameEvent currentEvent = eventQueue.GetNextEvent();
                currentEvent.SerializeForStream(stream);

                switch (currentEvent.EventType)
                {
                    case GameEvent.GameEventType.PlayerHit:
                        print("a player hit event is on the queue");
                        if (currentEvent.Target == Network.player)
                        {
                            print("processing hit to server player");
                            ProcessPlayerHit(NetworkDungeonController.GetNetworkPlayerObject(Network.player), ((PlayerHitGameEvent)currentEvent).damageReceived);
                        }
                        break;
                }

            }
        }
        else
        {

            int eventReceiveCount = 0;
            stream.Serialize(ref eventReceiveCount);


            for (int i = 0; i < eventReceiveCount; i++)
            {
                int readEventType = -1;
                stream.Serialize(ref readEventType);
                GameEvent.GameEventType readEventTypeValue = (GameEvent.GameEventType)readEventType;

                switch (readEventTypeValue)
                {
                    case GameEvent.GameEventType.PlayerHit:
                        print("processing a hit to a non-owned player");
                        PlayerHitGameEvent playerHitEvent = new PlayerHitGameEvent(stream);
                        ProcessPlayerHit(NetworkDungeonController.GetNetworkPlayerObject(playerHitEvent.Target), playerHitEvent.damageReceived);
                        break;
                    default:
                        throw new ArgumentException("GameEvents - invalid game event of type " + readEventTypeValue.ToString() + " received, no handler defined");
                }

            }

        }
    }

    void OnNetworkInstantiate(NetworkMessageInfo info)
    {
        if (this.networkView.isMine)
        {
            // I'm the server!
        }
    }

    void ProcessPlayerHit(GameObject targetPlayer, int damageReceived)
    {
        print("processing player hit");
        targetPlayer.GetComponent<CombatData>().currentHealth -= damageReceived;
    }

}
